﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FA_Server;
using FA_Model;
using System.Data;

namespace FixedAssets.Controls
{
    public partial class MDC_TableControl : System.Web.UI.UserControl
    {
       //static FA_S_MasterDataCard s_mdc;
       static DataTable t_mdc;
       static DataTable dq_mdc;
       TKFA.BLL.FA_MASTERDATA masterdata_bll = new TKFA.BLL.FA_MASTERDATA();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadAll();
            }
        }

        private void LoadAll()
        {
            #region
            TKFA.BLL.DEPARTMENT department_bll = new TKFA.BLL.DEPARTMENT();
            DropDownListDataBind(this.DropDownList1, department_bll.GetDepartment(), "SDEPARTMENTNAME", "UFIDACODE");
            //this.DropDownList1.DataSource = department_bll.GetDepartment();
            //this.DropDownList1.DataTextField = "SDEPARTMENTNAME";
            //this.DropDownList1.DataValueField = "UFIDACODE";
            //this.DropDownList1.DataBind();
            //this.DropDownList1.Items.Insert(0, "全部");
            #endregion

            #region
            TKFA.BLL.FA_DEVICECLASS_PROPERTY deviceclass_bll = new TKFA.BLL.FA_DEVICECLASS_PROPERTY();
            DropDownListDataBind(this.DropDownList2, deviceclass_bll.GetDeviceclass(), "SDEVICECLASS_NAME", "SDEVICECLASS_CODE");
            //this.DropDownList2.DataSource = deviceclass_bll.GetDeviceclass();
            //this.DropDownList2.DataTextField = "SDEVICECLASS_NAME";
            //this.DropDownList2.DataValueField = "SDEVICECLASS_CODE";
            //this.DropDownList2.DataBind();
            //this.DropDownList2.Items.Insert(0, "全部");
            #endregion

            #region
            DropDownListDataBind(this.DropDownList4, masterdata_bll.GetSKeeper(), "SKEEPER", "SKEEPER");
            //this.DropDownList4.DataSource = masterdata_bll.GetSKeeper();
            //this.DropDownList4.DataTextField = "SKEEPER";
            //this.DropDownList4.DataValueField = "SKEEPER";
            //this.DropDownList4.DataBind();
            //this.DropDownList4.Items.Insert(0, "全部");
            #endregion

            #region
            DropDownListDataBind(this.DropDownList5, masterdata_bll.GetsStoragesites(), "SSTORAGESITES", "SSTORAGESITES");
            //this.DropDownList5.DataSource = masterdata_bll.GetsStoragesites();
            //this.DropDownList5.DataTextField = "SSTORAGESITES";
            //this.DropDownList5.DataValueField = "SSTORAGESITES";
            //this.DropDownList5.DataBind();
            //this.DropDownList5.Items.Insert(0, "全部");
            #endregion

            #region

            t_mdc = masterdata_bll.GetAllData();

            t_mdc.PrimaryKey = new DataColumn[] { t_mdc.Columns["MD_ID"] };
            t_mdc.Columns.Add("ROWID", typeof(int));

            for (int i = 1; i <= t_mdc.Rows.Count; i++)
            {
                t_mdc.Rows[i - 1]["ROWID"] = i;
            }
            dq_mdc = t_mdc.Copy();
            C1DataBind(dq_mdc);
            #endregion
        }
        private void DropDownListDataBind(DropDownList ddl,DataTable binddt,string dtfield,string dvfield)
        {
            DataRow dr = binddt.NewRow();
            dr[0] = "全部";
            if (!dtfield.Equals(dvfield))
            {
                dr[1] = "全部";
            }
            binddt.Rows.InsertAt(dr,0);
            ddl.DataSource = binddt;
            ddl.DataTextField = dtfield;
            ddl.DataValueField = dvfield;
            ddl.DataBind();
        }

        private void C1DataBind(DataTable dt)
        {
            this.C1GridView1.DataSource = dt;
            this.C1GridView1.DataBind();
        }
        private void C1DataBind(DataTable dt,string swhere)
        {
            DataView dv = t_mdc.DefaultView;
            dv.RowFilter = swhere;
            dq_mdc = dv.ToTable();
            DropDownListDataBind(this.DropDownList1, dq_mdc.DefaultView.ToTable(true, new string[] { "SDEPARTMENTNAME", "SDEPARTMENTCODE" }), "SDEPARTMENTNAME", "SDEPARTMENTCODE");
            DropDownListDataBind(this.DropDownList2, dq_mdc.DefaultView.ToTable(true, new string[] { "SDEVICECLASS_NAME", "SDEVICECLASS_CODE" }), "SDEVICECLASS_NAME", "SDEVICECLASS_CODE");
            DropDownListDataBind(this.DropDownList4, dq_mdc.DefaultView.ToTable(true, new string[] { "SKEEPER" }), "SKEEPER", "SKEEPER");
            DropDownListDataBind(this.DropDownList5, dq_mdc.DefaultView.ToTable(true, new string[] { "SSTORAGESITES" }), "SSTORAGESITES", "SSTORAGESITES");
            C1DataBind(dq_mdc);
        }

        protected void C1GridView1_Sorting(object sender, C1.Web.Wijmo.Controls.C1GridView.C1GridViewSortEventArgs e)
        {
            C1DataBind(dq_mdc);
        }

        protected void C1GridView1_PageIndexChanging(object sender, C1.Web.Wijmo.Controls.C1GridView.C1GridViewPageEventArgs e)
        {
            this.C1GridView1.PageIndex = e.NewPageIndex;
            C1DataBind(dq_mdc);
        }

        protected void C1GridView1_ColumnGrouped(object sender, C1.Web.Wijmo.Controls.C1GridView.C1GridViewColumnGroupedEventArgs e)
        {
            C1DataBind(dq_mdc);
        }

        protected void C1GridView1_ColumnUngrouped(object sender, C1.Web.Wijmo.Controls.C1GridView.C1GridViewColumnUngroupedEventArgs e)
        {

        }
       

        protected void DropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<string> strlist = new List<string> ();
            string strtjheader = "选择条件";
            if (!this.DropDownList1.SelectedValue.Equals("全部"))
            {
                strlist.Add("  SDEPARTMENTCODE='" + this.DropDownList1.SelectedValue + "'");
                strtjheader += "--部门（"+this.DropDownList1.SelectedItem.Text+"）";
            }
            if (!this.DropDownList2.SelectedValue.Equals("全部"))
            {
                strlist.Add("  SDEVICECLASS_CODE='" + this.DropDownList2.SelectedValue + "'");
                strtjheader += "--设备类别（" + this.DropDownList2.SelectedItem.Text + "）";
            }
            if (!this.DropDownList4.SelectedValue.Equals("全部"))
            {
                strlist.Add("  SKEEPER='" + this.DropDownList4.SelectedValue + "'");
                strtjheader += "--保管人（" + this.DropDownList4.SelectedItem.Text + "）";
            }
            if (!this.DropDownList5.SelectedValue.Equals("全部"))
            {
                strlist.Add("  SSTORAGESITES='" + this.DropDownList5.SelectedValue + "'");
                strtjheader += "--存放地点（" + this.DropDownList5.SelectedItem.Text + "）";
            }
            C1DataBind(null,string.Join(" and ",strlist.ToArray()));
            tjheader.InnerText = strtjheader;
        }
    }
}